package elephant.rpc.server.message;

import java.util.Arrays;

/**
 * 
 * @author skydu
 *
 */
public class RPCMessage {
	//
	public static final byte TYPE_AUTH=1;//auth for session
	public static final byte TYPE_REQ=2;
	public static final byte TYPE_RESP=3;
	public static final byte TYPE_PUSH=4;
	//
	public static final String AUTH_SIGN="7B797786EAFAC89B3EB7531E5A7561BC";
	//
	public long id;
	public byte type;
	public Object [] payloads;
	public String[] parameterTypes;
	public Object[] parameters;
	public Boolean isAsync;
	public Long sendTime;
	public Long responseTime;
	//
	public RPCMessage(){
	}
	//
	public RPCMessage(byte type){
		this.type=type;
	}
	//
	public static RPCMessage createReqMsg(long id,String serviceClass,String methodName,
			String[] parameterTypes,Object [] parameters){
		RPCMessage msg=new RPCMessage(TYPE_REQ);
		msg.id=id;
		msg.payloads=new Object[2];
		msg.payloads[0]=serviceClass;
		msg.payloads[1]=methodName;
		msg.parameterTypes=parameterTypes;
		msg.parameters=parameters;
		return msg;
	}
	//
	public static RPCMessage createRspMsg(long id,Object retValue,int bizExceptionCode,
			String bizExceptionMessage,RuntimeException otherException){
		RPCMessage msg=new RPCMessage(TYPE_RESP);
		msg.id=id;
		msg.payloads=new Object[4];
		msg.payloads[0]=retValue;
		msg.payloads[1]=bizExceptionCode;
		msg.payloads[2]=bizExceptionMessage;
		msg.payloads[3]=otherException;
		return msg;
	}
	//
	public static String getServiceClass(RPCMessage msg){
		return msg.payloads[0].toString();
	}
	//
	public static String getMethodName(RPCMessage msg){
		return msg.payloads[1].toString();
	}
	//
	public static Object getRetValue(RPCMessage msg){
		return msg.payloads[0];
	}
	//
	public static int getBizExceptionCode(RPCMessage msg){
		return (int) msg.payloads[1];
	}
	//
	public static String getBizExceptionMessage(RPCMessage msg){
		return (String) msg.payloads[2];
	}
	//
	public static RuntimeException getOtherException(RPCMessage msg){
		return (RuntimeException) msg.payloads[3];
	}
	//
	@Override
	public String toString() {
		return "RPCMessage [id=" + id + ", type=" + type + ", payloads=" + Arrays.toString(payloads)
				+ ", parameterTypes=" + Arrays.toString(parameterTypes) + ", parameters=" + Arrays.toString(parameters)
				+ ", isAsync=" + isAsync + "]";
	}
}
